package cn.tannn.ops.pwd.controller.dto;

import cn.tannn.jdevelops.result.bean.SerializableBean;
import cn.tannn.ops.pwd.enums.EncryptType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.Optional;

/**
 * 新增密码
 *
 * @author tnnn
 * @version V1.0
 * @date 2023-02-20 09:28
 */
@Getter
@Setter
@ToString
@Schema(description = "新增密码")
public class CreateKeePass extends SerializableBean<CreateKeePass> {

    /**
     * 分组
     */
    @Schema(description = "分组")
    String groupings;

    /**
     * 密码标题
     */
    @NotBlank
    @Schema(description = "密码标题", requiredMode = Schema.RequiredMode.REQUIRED)
    String title;

    /**
     * 用户名
     */
    @Schema(description = "用户名")
    String username;

    /**
     * 明文密码
     */
    @NotBlank
    @Schema(description = "明文密码", requiredMode = Schema.RequiredMode.REQUIRED)
    String password;

    /**
     * 密码相关地址
     */
    @Schema(description = "密码相关地址")
    String url;

    /**
     * 备注
     */
    @Schema(description = "备注")
    String remark;

    /**
     * 加密方式
     * RSA,AES,DES,HS256
     *
     * @see EncryptType
     */
    @NotBlank
    @Schema(description = "加密方式", requiredMode = Schema.RequiredMode.REQUIRED)
    String encryptTypeStr;

    /**
     * 密钥 - 用户自己要保存好 [RSA用公钥]
     */
    @NotBlank
    @Schema(description = "密钥", requiredMode = Schema.RequiredMode.REQUIRED)
    String secret;

    /**
     * 加密次数
     */
    @Schema(description = "加密次数")
    Integer encryptCount;

    public String getGroupings() {
        if (StringUtils.isBlank(groupings)) {
            return "根目录";
        }
        return groupings;
    }

    public Integer getEncryptCount() {
        return Optional.ofNullable(encryptCount).orElse(1);
    }
}
